package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.MemberFee;
import com.tbit.uqbike.client.pojo.MemberLog;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.GeoService;
import com.tbit.uqbike.client.service.MemberFeeService;
import com.tbit.uqbike.client.service.MemberLogService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.DateTimeUtil;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 免押会员
 *
 * @author Leon
 * 2019年9月23日 下午7:29:05
 */
@Controller
@RequestMapping("/memberFee")
public class MemberFeeController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MemberFeeService memberFeeService;
    @Autowired
    private MemberLogService memberLogService;
    @Autowired
    private GeoService geoService;

    /**
     * 获取会员费用
     *
     * @param accountId   品牌id
     * @param lo          经度
     * @param la          纬度
     * @param mapType     地图类型
     * @param adAccountId 区域id
     * @param token       令牌
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/findMemberFee")
    public void findMemberFee(Integer accountId, Double lo, Double la, Integer mapType, Integer adAccountId, String token, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            //Assert.notNull(adAccountId, "-10001", "品牌id不能为空");
            if (adAccountId == null) {
                Assert.notNull(lo, "-2001", "");
                Assert.notNull(la, "-2002", "");
                Integer agentId = geoService.getBelongAccount(accountId, lo, la, mapType);
                adAccountId = agentId;
            }
            List<MemberFee> memberFees = new ArrayList<>();
            if (adAccountId != null) {
                memberFees = memberFeeService.find(adAccountId);
            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, memberFees);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 查询免押会员购买记录
     *
     * @param accountId   品牌id
     * @param lo          经度
     * @param la          纬度
     * @param mapType     地图类型
     * @param adAccountId 区域id
     * @param token       令牌
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/findHistory")
    public void findHistory(Integer accountId, Double lo, Double la, Integer mapType, Integer adAccountId, String token, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            User user = tokenService.getUser(token);
            //Assert.notNull(adAccountId, "-10001", "品牌id不能为空");
            if (adAccountId == null && lo != null && la != null) {
                Integer agentId = geoService.getBelongAccount(accountId, lo, la, mapType);
                adAccountId = agentId;
            }
            List<MemberLog> memberLogs = new ArrayList<>();
            if (adAccountId != null) {
                memberLogs = memberLogService.findByUserId(user.getUserId(), adAccountId);
            }
            if (memberLogs.size() > 0) {
                for (MemberLog m : memberLogs) {
                    if (DateTimeUtil.StringToDateTime(m.getExpireTime() + " 23:59:59").getTime() < System.currentTimeMillis()) {
                        m.setIsValid(0);
                    } else {
                        m.setIsValid(1);
                    }
                }
            }
            //List<MemberLog> memberLogs = memberLogService.findByUserId(user.getUserId(),adAccountId);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, memberLogs);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 分页获取会员费用
     *
     * @param adAccountId 区域id
     * @param token       令牌
     * @param rowCount    页码数
     * @param pageNO      每页大小
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/query")
    public void query(Integer adAccountId, String token, Integer rowCount, Integer pageNO, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /*默认值*/
            if (rowCount == null || rowCount < 1) {
                rowCount = 10;
            }
            if (pageNO == null || pageNO < 1) {
                pageNO = 1;
            }
            Assert.notNull(adAccountId, "-10001", "品牌id不能为空");
            /* 校验 */
            User user = tokenService.getUser(token);
            List<MemberFee> memberFees = memberFeeService.query(adAccountId, pageNO, rowCount);

            Integer total = memberFeeService.count(adAccountId);
            map.put("total", total);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, memberFees);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }
}
